<?php
/**
 * 《电话区号》实现类
 *
 * @author 孙晓晔
 * @version $Id: Mobile.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/TelUtil.class.php');

class CdrCt extends TelUtil {

	public $tbl = Constant::tbl_cdr_ct;

	public $orderby = array(
		'-' => '请选择',
		'mobile' => '号段',
		'province' => '省份',
		'city' => '城市',
		'1' => '创建时间',
		'2' => '更新时间',
	);
	
	public $groupby = array(
		'-' => '请选择',
		'dst' => '被叫',
			
		'1' => '小时',
		'2' => '日期',
		'3' => '月份',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		$province = $this->code_option('4');
		$controller->assign_by_ref('province_option', $province);

		$controller->display($request, 'tel/cdr/ct/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == '`join`') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['join'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {
		$province_s = $request['province_s'];
		$mobile_s = substr($request['mobile_s'], 0, 7);

		$where = " WHERE 1=1";

		if (!empty($province_s) && $province_s <> '-') {
			$where .= " AND code = '" . $province_s . "'";
		}

		if (!empty($mobile_s)) {
			$where .= " AND mobile LIKE '" . $mobile_s . "%'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();		
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];

				$row['category'] = $this->category[$row['category']];

				$row['status_t'] = $this->status[$row['status']];

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'tel/cdr/ct/list');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'tel/mobile/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$mobile = $request['mobile'];
		$province = $request['province'];
		$city = $request['city'];
		$zone = $request['zone'];
		$post = $request['post'];

		$status = $request['status'];
			
		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(mobile, province, city, zone, post, status, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $mobile, $province, $city, $zone, $post, $status);
		// debug($sql);
		$db->query($sql);

		$request['id_s'] = $db->insert_id();
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'tel/mobile/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$mobile = $request['mobile'];
		$province = $request['province'];
		$city = $request['city'];
		$zone = $request['zone'];
		$post = $request['post'];

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET mobile = '%s', province = '%s', city = '%s', zone = '%s', post = '%s', modifytime = now() WHERE id = %s";
		$sql = sprintf($fmt, $this->tbl, $mobile, $province, $city, $zone, $post, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function copy($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		$row = $this->_view($id);
	
		$controller->assign_by_ref('result', $row);
	
		$controller->display($request, 'tel/mobile/copy');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'tel/cdr/ct/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		} 
		
		$row['rule_t'] = $this->rule[$row['rule']];
		
		$row = $this->_view_t($row);

		return $row;
	}
	
	function import($controller, $request) {
		$this->pretreat($controller);
	
		$import = array(
				'-' => '请选择',
				'1' => '文本方式',
				'2' => '文件方式',
		);
	
		$controller->assign_by_ref('import_option', $import);
	
		$controller->display($request, 'tel/cdr/ct/import');
		return true;
	}
	
	function ajax_upload($controller, $request) {
		require_once 'HTTP/Upload.php';
		$upload = new HTTP_Upload('en');
		$file = $upload->getFiles('Filedata');
		if (PEAR::isError($file)) {
			debug($file->getMessage());
		}
	
		$oper = $request['oper'];
		$name = $request['name'];
		$type = $request['type'];
	
		if ($file->isValid()) {
			$filename = $file->upload['tmp_name'];
			debug($filename);
	
			$this->_txt($filename);
		} else {
			debug($file->getMessage());
		}
	
		echo "1";
		return true;
	}
	
	function _txt($file) {
		$this->_txt_1($file);
	}
	
	function a($controller, $request) {
		$file = "/Users/sunxiaoye/Desktop/cccc/a.txt";
		
		$this->_txt_1($file);
	
		logdate($controller, $request);
		return true;
	}
	
	function _txt_1($file) {
		$fd = fopen($file, "r");
		while (!feof($fd)) {
			$buf = $this->_to_utf8(fgets($fd));
			debug($buf);
			$this->_line_1($buf);
		}
		fclose($fd);
	}
	
	function _line_1($buf) {
		$buf = trim($buf);
		if (strlen($buf) < 5) {
			return;
		}
		
		$a = preg_split("/\t/", $buf);
		// preg_match("/(\s,)/", $name, $a);
		
		print_r($a);
		if (count($a) < 2) {
			return;
		}
		
		$name = trim($a[0]);
			
		if (!preg_match("/^(市话|国内|国际)/", $name)) {
			return;
		}
			
		$dst = trim($a[1]);
		// $createtime = sprintf("%s %s", trim($a[2]), trim($a[3]));
		$createtime = trim($a[2]);
		
		$billtime = trim($a[3]);
		$cost = trim($a[4]);
		$type = trim($a[5]);
		$content = '';
		// $content = trim($a[7]);
		
		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE dst = '%s' AND createtime = '%s'";
		$sql = sprintf($fmt, $this->tbl, $dst, $createtime);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			return;
		} else {
			$fmt = "INSERT INTO %s(name, dst, billtime, cost, type, content, createtime) VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s')";
			$sql = sprintf($fmt, $this->tbl, $name, $dst, $billtime, $cost, $type, $content, $createtime);
			debug($sql);
			$db->query($sql);
		}
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function importPost($controller, $request) {
		$this->pretreat($controller);

		$data = $request['data'];
		// echo $data;

		$aa = preg_split("/[\\n|\\n\\r]+/i", $data);
		// print_r($a);

		
		while (list($k, $buf) = each($aa)) {
			$this->_line_1($buf);
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function group($controller, $request) {
		$this->pretreat_group($controller);
	
		// $controller->assign('t1', strtotime('-1 month'));
		// $controller->assign('t2', strtotime('+1 day'));
	
		$num = array(
				'1' => '按次数',
				'2' => '按金额',
		);
		$controller->assign('num_option', $num);
	
		$controller->display($request, 'tel/cdr/ct/group');
		return true;
	}
	
	function groupby($controller, $px) {
		$px = $this->groupby_aa($px, $this->groupby, 'dst');
	
		$controller->assign('groupby', $this->groupby[$px]);
	
		return $this->groupby_dd($px);
	}
	
	function groupPost($controller, $request) {
		$this->pretreat($controller);
			
		$this->groupPara($controller, $request);
	
		$num_s = $request['num_s'];
		if (!empty($num_s) && $num_s == '2') {
			$fmt = "SELECT %s AS a, SUM(cost) AS c FROM %s a ";
		} else {
			$fmt = "SELECT %s AS a, COUNT(cost) AS c FROM %s a ";
		}
		$sql = sprintf($fmt, $this->s, $this->tbl);
	
		$where = $this->_where($request);
	
		$request['sql'] = sprintf("%s %s GROUP BY %s ORDER BY %s %s", $sql, $where, $this->s, $this->orderby, $this->desc);
	
		$this->_group($controller, $request);
	
		// $controller->display($request, 'list');
		return true;
	}
	
	function _name($row, $px) {
		
	
		return $row;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function t($controller, $request) {
		$this->pretreat($controller);

		$s = "13532789657";
		$s = "9118080085263972484";
		$s = "9966880085263972484";

		$a = $this->_number($s);
		
		print_r($a);

		logdate($controller, $request);
		return true;
	}
}
?>